package org.jeecg.modules.cascv.image.util;

import cn.hutool.core.util.RandomUtil;

import java.util.*;

public class MergeUtil {

    public static Long[][] mergeRange(List<String> ranges, int mergeRange){
        int size = ranges.size();
        Long[][] intervals = new Long[size][2];
        for (int i = 0; i < size; i++) {
            String p = ranges.get(i);
            String[] arr = p.split(",");
            intervals[i] = new Long[]{Long.parseLong(arr[0]), Long.parseLong(arr[1])};
        }
        return mergeRange(intervals, mergeRange);
    }

    public static Long[][] mergeRange(Long[][] intervals, int mergeRange) {
        if(intervals.length==0) return new Long[0][0];
        List<Long[]> ans = new ArrayList<>();
        Long[] cur;
        sort(intervals,0,intervals.length-1);
        cur=intervals[0];
        for(int i=1; i<intervals.length; i++){
            if(intervals[i][0] - cur[1] <= mergeRange){
                if(intervals[i][1]>cur[1]) {
                    cur[1]=intervals[i][1];
                }else{
                    continue;
                }
            }else{
                ans.add(cur);
                cur=intervals[i];
            }
        }
        ans.add(cur);

        return ans.toArray(new Long[ans.size()][]);
    }
    public static void sort(Long[][] intervals,int l,int r){
        if(l>=r) return;
        int p=partition(intervals,l,r);
        sort(intervals,l,p-1);
        sort(intervals,p+1,r);
    }
    public static int partition(Long[][] intervals,int l,int r){
        Long[] temp = intervals[l];
        while(l<r){
            while(l<r){
                if(intervals[r][0]<temp[0]){
                    intervals[l]=intervals[r];
                    break;
                }
                r--;
            }
            while(l<r){
                if(intervals[l][0]>temp[0]){
                    intervals[r]=intervals[l];
                    break;
                }
                l++;
            }
        }
        intervals[l]=temp;
        return l;
    }



    public static Map<Long, Long> merge(List<Long> list, int mergeRange){
        long start = System.currentTimeMillis();
//        Collections.sort(list, Long::compareTo);
        Long[] arr = new Long[list.size()];

        list.toArray(arr);
        //System.out.println(System.currentTimeMillis() - start);
        return merge(arr, mergeRange);
    }

    public static Map<Long, Long> merge(Long[] arr, int mergeRange){
        long start = System.currentTimeMillis();
        Arrays.sort(arr);
        //System.out.println(System.currentTimeMillis() - start);
        Map<Long, Long> range = new LinkedHashMap<>();
        Long last_start = arr[0];
        Long last = arr[0];
        Long curr = 0L;
        int len = arr.length;
        range.put(arr[0], arr[0]);
        for (int i = 1; i < len; i++) {
            curr = arr[i];
            if(curr - last > mergeRange){
                last_start = curr;
            }
            range.put(last_start, curr);
            last = curr;
        }
        return range;

    }
    public static void main(String[] args) {
        int len = 40000;
        long[] arr = new long[len];
        for (int i = 0; i < len; i++) {
            arr[i] = RandomUtil.randomLong(100000);
        }
        long start = System.currentTimeMillis();
        Arrays.sort(arr);
        System.out.println(System.currentTimeMillis() - start);
        Arrays.sort(arr);
        System.out.println(System.currentTimeMillis() - start);
    }
}
